import { getLabel, addLabel, delLabel } from '@/api/label'

export default {
    namespaced: true,
    state: {
        labelList: []
    },
    actions: {
        getLabel({ commit }, payload) {
            commit('getLabel', payload)
        },
        addLabel({ commit }, payload) {
            commit('addLabel', payload)
        },
        delLabel({ commit }, payload) {
            commit('delLabel', payload)
        }
    },
    mutations: {
        getLabel(state, payload) {
            getLabel(payload).then(res => {
                state.labelList = res
            }).catch(err => {
                console.log('err', err)
            })
        },
        addLabel(state, payload) {
            addLabel(payload).then(res => {
                console.log('addLabelList: ', res)
                state.labelList.push(payload.message)
            }).catch(err => {
                console.log('err', err)
            })
        },
        delLabel(state, payload) {
            delLabel(payload).then(res => {
                console.log('delLabel: ', res)
                var index = state.labelList.indexOf(payload.message)
                if (index > -1) {
                    state.labelList.splice(index, 1)
                }
            }).catch(err => {
                console.log('err', err)
            })
        }
    },
    getters: {
        labelList: (state) => { return state.labelList }
    }
}